import java.util.concurrent.Executors;

import javax.jms.*;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Producer {
    // URL of the JMS server. DEFAULT_BROKER_URL will just mean
    // that JMS server is on localhost
    private static String url = ActiveMQConnection.DEFAULT_BROKER_URL;

    // Name of the queue we will be sending messages to
    private static String subject = "TESTQUEUE";
    private static String subject2 = "EXTESTQUEUE";
    public static void main(String[] args) throws JMSException {
    	
    	
        // Getting JMS connection from the server and starting it
        ConnectionFactory connectionFactory =     new ActiveMQConnectionFactory(url);
        Connection connection = connectionFactory.createConnection();
        connection.start();

        // JMS messages are sent and received using a Session. We will
        // create here a non-transactional session object. If you want
        // to use transactions you should set the first parameter to 'true'
        Session session = connection.createSession(false,       Session.AUTO_ACKNOWLEDGE);

        // Destination represents here our queue 'TESTQUEUE' on the
        // JMS server. You don't have to do anything special on the
        // server to create it, it will be created automatically.
        Destination destination = session.createQueue(subject);

        // MessageProducer is used for sending messages (as opposed
        // to MessageConsumer which is used for receiving them)
          final MessageProducer producer = session.createProducer(destination);

        // We will send a small text message saying 'Hello' in Japanese
          final TextMessage message = session.createTextMessage("local pin");
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
      final int i=50;
     
      final int j=50;
        

    	Executors.newSingleThreadExecutor().execute(new Runnable(){
   
    	    public void run(){ int count_local = 0;  
    	        while(true){
    	        	       	
    	        	try {
    	        		if(count_local<j)
    	        		{  
    	        			producer.send(message);
    	        		 
    	        		Thread.sleep(10);
    	        		count_local++;
    	        		}
					
					} catch (InterruptedException | JMSException e) {
					
						e.printStackTrace();
					}
    	        	
    	        }
    	    }
    	});
    	
      
    	Executors.newSingleThreadExecutor().execute(new Runnable(){

    	    public void run(){ 
    	    	  int count_ext = 0;	
    	        while(true){
try {
    	            ConnectionFactory connectionFactory2 =     new ActiveMQConnectionFactory(url);
    	            Connection connection2 = connectionFactory2.createConnection();
    	            connection2.start(); 
    	          
    	          Session session2 = connection2.createSession(false,       Session.AUTO_ACKNOWLEDGE);
    	          Destination destination2 = session2.createQueue(subject2);

    	         
    	           MessageProducer producer2 = session2.createProducer(destination2);

    	          // We will send a small text message saying 'Hello' in Japanese
    	           TextMessage message2;
				
					message2 = session2.createTextMessage("IMPS");
    	        		
						if(count_ext<i)
    	        		{	
    	        			count_ext++;
    	        	
		
    	        			producer2.send(message2);
    	        	    System.out.println("Sent message '" + message2.getText() + "'");
 	        	    
    	        		Thread.sleep(5);
    	    	        		}
			 
						
						
					} catch (InterruptedException | JMSException e) {
					
						e.printStackTrace();
					}
    	        	
    	        }
    	    }
    	});
    	
    	
    	
    	   	
    	
    	
    	
    	
    	
    	
    	
        
        
      
		
        
        
        
        


        
        
        
        
        
      

      //  connection.close();
        
        
        
        
    //    connection2.close();
      
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
        
    }
}